/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.page.account.edit;

import hu.glassy.core.model.Account;
import hu.glassy.core.service.AccountService;
import hu.glassy.web.auth.ApplicationSession;
import hu.glassy.web.error.ActivationTokenException;
import hu.glassy.web.page.account.registration.AccountActivationPage;
import hu.glassy.web.page.common.PublicPage;
import hu.glassy.web.panel.account.edit.AccountDataInputPanel;
import hu.glassy.web.panel.account.edit.ChangePasswordPanel;
import hu.glassy.web.panel.common.GlassyPanel;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * Ezen az oldalon módosíthatja a felhasználó a személyes adatait
 *
 * @author molnar.alexander@sonrisa.hu
 */
@AuthorizeInstantiation(Roles.USER)
public class AccountEditPage extends PublicPage
{

  private static final long serialVersionUID = 8169867215114481242L;

  @SpringBean
  private AccountService accountService;

  public AccountEditPage(final PageParameters parameters)
  {
    super(parameters);
  }

  @Override
  public IModel<String> getPageTitleModel()
  {
    return new ResourceModel("account.edit.title");
  }

  @Override
  public Panel getContentPanel(final String panelId, final PageParameters parameters)
  {
    return generateInputPanel(panelId);
  }

  // --------------------------------------------------
  // ~ Private methods
  // --------------------------------------------------
  private Panel generateInputPanel(final String panelId)
  {
    return new GlassyPanel<Account>(panelId)
    {
      private static final long serialVersionUID = 4858465942516511996L;

      @Override
      public IModel<String> getTitle()
      {
        return new ResourceModel("account.edit.title");
      }

      @Override
      public Component getContent(final String componentId)
      {
        return generateDataInputPanel(componentId);
      }
    };
  }

  private Component generateDataInputPanel(final String panelId)
  {
    return new AccountDataInputPanel(panelId, ApplicationSession.get().getAccount())
    {
      private static final long serialVersionUID = 8790960804175108811L;

      @Override
      public IModel<String> getButtonLabel()
      {
        return new ResourceModel("account.edit.submit");
      }

      @Override
      protected boolean isUsernameInputVisible()
      {
        return false;
      }

      @Override
      protected boolean isPasswordInputVisible()
      {
        return false;
      }

      @Override
      protected boolean isConfirmInputVisible()
      {
        return false;
      }

      @Override
      public Component getFooter(final String componentId)
      {
        return new ChangePasswordPanel(componentId);
      }

      @Override
      public void onFormSubmit(final AjaxRequestTarget target, final IModel<Account> model)
      {
        try
        {
          final Account account = model.getObject();
          accountService.updateAccount(account);

          success(getString("account.edit.success"));

          if (!account.isActive())
          {
            getSession().info(getString("account.edit.emailchange"));
            setResponsePage(AccountActivationPage.class);
          }
        }
        catch (final ActivationTokenException e)
        {
          error(e.getMessage());
        }
      }
    };
  }
}
